package com.atguigu.gmall.cart.api;


import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @DeleteMapping("/deleteSku")
    Result deleteSkuFromCart(@RequestBody List<String> skuIds,
                             HttpServletRequest request){

        String key = cartService.determinCartKey(request);
        if (!CollectionUtils.isEmpty(skuIds)){

            cartService.deleteCartItemBatch(key,skuIds);
        }

        return Result.ok();
    }


    @DeleteMapping("/deleteCart/{skuId}")
    public Result ajaxDeleteCartItem(@PathVariable("skuId") Long skuId,
                                     HttpServletRequest request){
        String key = cartService.determinCartKey(request);
        cartService.deleteCartItem(key,skuId);
        return Result.ok();
    }


    @GetMapping("/checkCart/{skuId}/{checked}")
    public Result ajaxCheckCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("checked") Integer checked,
                                HttpServletRequest request){
        String key = cartService.determinCartKey(request);
        cartService.checkedCartItem(key,skuId,checked);
        return null;
    }


    /**
     * ajax添加购物车
     * @param skuId
     * @param skuNum
     * @param request
     * @return
     */
    @PostMapping("/addToCart/{skuId}/{skuName}")
    public Result ajaxAddToCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("skuNum")Integer skuNum,
                                HttpServletRequest request){
        //ajax请求过了网关，网关会自动提取并透传用户id以及临时id
        String key = cartService.determinCartKey(request);
        CartInfo cartInfo = cartService.addToCart(key, skuId, skuNum);
        return Result.ok(cartInfo);
    }

    /**
     * 表示把哪个商品添加到购物车
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/addToCart")
    public CartInfo addToCart(@RequestParam("skuId") Long skuId,
                             @RequestParam("skuNum") Integer skuNum,
                             HttpServletRequest request){

        /**
         * 告诉我们操作哪个用户的购物车
         * 用户登录了：有cookie
         *      token：
         *      用户没登录： 有Cookie
         *          userTempId
         *
         */
        //操作redis完整的key
        String key = cartService.determinCartKey(request);
        CartInfo skuInfo  = cartService.addToCart(key,skuId,skuNum);

        return skuInfo;

    }

    /**
     *  返回购物车中所有商品
     * @return
     */
    @GetMapping("/listCart")
    public Result listCart(HttpServletRequest request){

        String userCartKey = cartService.userCartKey(request);
        String tempCartKey = cartService.tempCartKey(request);

        //查询所有购物车数据
        List<CartInfo> cartInfos = cartService.listCart(userCartKey,tempCartKey);
        //把查到的购物车数据返回
        return Result.ok(cartInfos);
    }

    /**
     * 获取购物车中已经选中的商品
     * @return
     */
    @GetMapping("/cartListChecked")
    public List<CartInfo> cartListChecked(HttpServletRequest request){
        //确定用哪个key
        String key = cartService.determinCartKey(request);
        return cartService.listCheckCartInfo(key);
    }
}
